const SearchAlgorithms = require("../template")

function testSearchAlgorithmsBasic() {
  const searcher = new SearchAlgorithms()

  console.log(
    "1. 线性搜索基础测试:",
    (() => {
      const arr = [10, 20, 30, 40, 50, 60, 70, 80, 90]
      return searcher.linearSearch([...arr], 40) === 3
    })()
  )

  console.log(
    "2. 线性搜索未找到:",
    (() => {
      const arr = [10, 20, 30, 40, 50, 60, 70, 80, 90]
      return searcher.linearSearch([...arr], 25) === -1
    })()
  )

  console.log(
    "3. 线性搜索首元素:",
    (() => {
      const arr = [10, 20, 30, 40, 50, 60, 70, 80, 90]
      return searcher.linearSearch([...arr], 10) === 0
    })()
  )

  console.log(
    "4. 线性搜索尾元素:",
    (() => {
      const arr = [10, 20, 30, 40, 50, 60, 70, 80, 90]
      return searcher.linearSearch([...arr], 90) === 8
    })()
  )

  console.log(
    "5. 二分搜索基础测试:",
    (() => {
      const arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
      return searcher.binarySearch([...arr], 7) === 3
    })()
  )

  console.log(
    "6. 二分搜索未找到:",
    (() => {
      const arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
      return searcher.binarySearch([...arr], 4) === -1
    })()
  )

  console.log(
    "7. 二分搜索首元素:",
    (() => {
      const arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
      return searcher.binarySearch([...arr], 1) === 0
    })()
  )

  console.log(
    "8. 二分搜索尾元素:",
    (() => {
      const arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
      return searcher.binarySearch([...arr], 19) === 9
    })()
  )

  console.log(
    "9. 空数组线性搜索:",
    (() => {
      return searcher.linearSearch([], 5) === -1
    })()
  )

  console.log(
    "10. 空数组二分搜索:",
    (() => {
      return searcher.binarySearch([], 5) === -1
    })()
  )

  console.log(
    "11. 单元素线性搜索:",
    (() => {
      return searcher.linearSearch([42], 42) === 0
    })()
  )

  console.log(
    "12. 单元素二分搜索:",
    (() => {
      return searcher.binarySearch([42], 42) === 0
    })()
  )

  console.log(
    "13. 单元素搜索未找到:",
    (() => {
      return searcher.linearSearch([42], 43) === -1 && searcher.binarySearch([42], 43) === -1
    })()
  )

  console.log(
    "14. 重复元素线性搜索:",
    (() => {
      const arr = [1, 2, 2, 3, 3, 3, 4, 5]
      return searcher.linearSearch([...arr], 2) === 1 // 返回第一个匹配的位置
    })()
  )

  console.log(
    "15. this上下文搜索测试:",
    (() => {
      const searchObj = {
        multiplier: 3,
        processSearch: function(arr, target) {
          const searcher = new SearchAlgorithms()
          const result = searcher.linearSearch(arr, target)
          return result * this.multiplier
        }
      }
      return searchObj.processSearch([1, 2, 3, 4, 5], 3) === 6 // 2 * 3 = 6
    })()
  )

  console.log(
    "16. 复杂对象this绑定测试:",
    (() => {
      const searchConfigObj = {
        config: { offset: 10 },
        processWithConfig: function(arr, target) {
          const searcher = new SearchAlgorithms()
          const result = searcher.binarySearch(arr, target)
          return result + this.config.offset
        }
      }
      return searchConfigObj.processWithConfig([1, 3, 5, 7, 9], 5) === 12 // 2 + 10 = 12
    })()
  )
}

testSearchAlgorithmsBasic()
